﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class CreateCustomer : Form
    {
        public CreateCustomer()
        {
            InitializeComponent();
        }

        //Allow only characters to be entered
        void OnlyCharacters_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
        }

        //Allow only numbers to be entered
        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        //Create Customer
        private void btnCreateCustomerCreate_Click(object sender, EventArgs e)
        {
            bool create = false;
            if (string.IsNullOrEmpty(tbxCreateCustomerNRIC.Text))
            {
                MessageBox.Show("You are Missing NRIC");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerFirstName.Text))
            {
                MessageBox.Show("You are Missing First Name");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerFirstName.Text))
            {
                MessageBox.Show("You are Missing Last Name");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerLastName.Text))
            {
                MessageBox.Show("You are Missing Occupation");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerPlaceOfEmployment.Text))
            {
                MessageBox.Show("You are Missing Place Of Employment");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerNationality.Text))
            {
                MessageBox.Show("You are Missing Nationality");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerContactNumber.Text))
            {
                MessageBox.Show("You are Missing Contact Number");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerEmail.Text))
            {
                MessageBox.Show("You are Missing Email");
            }
            else if (string.IsNullOrEmpty(tbxCreateCustomerAddress.Text))
            {
                MessageBox.Show("You are Missing Address");
            }
            else
            {
                Customer cm = new Customer();
                cm.Nric = tbxCreateCustomerNRIC.Text;
                cm.FirstName = tbxCreateCustomerFirstName.Text;
                cm.LastName = tbxCreateCustomerLastName.Text;
                cm.Occupation = tbxCreateCustomerOccupation.Text;
                cm.PlaceOfEmployment = tbxCreateCustomerPlaceOfEmployment.Text;
                cm.Nationality = tbxCreateCustomerNationality.Text;
                cm.ContactNumber = tbxCreateCustomerContactNumber.Text;
                cm.Email = tbxCreateCustomerEmail.Text;
                cm.HomeAddress = tbxCreateCustomerAddress.Text;
                DBManager.CreateCustomer(cm);
                create = true;
            }
            if (create == true)
            {
                MessageBox.Show("Create Successful!");
                ClearTextBoxes(this.Controls);
            }
        }

        private void btnCreateCustomerCheck_Click(object sender, EventArgs e)
        {
            bool result = DBManager.CheckCustomerNric(tbxCreateCustomerNRIC.Text);
            if (result == false)
            {
                MessageBox.Show("Customer NRIC is available");         
            }
            else
            {                
                MessageBox.Show("Customer NRIC is not available");  
            }
        }
        //Clear textboxes after create
        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }

    }
}
